

package com.kimo.shop.dao;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kimo.shop.bean.app.dto.MyOrderDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kimo.shop.bean.app.dto.MyOrderItemDto;
import com.kimo.shop.bean.app.dto.OrderCountData;
import com.kimo.shop.bean.distribution.UserShoppingDataDto;
import com.kimo.shop.bean.model.Order;
import com.kimo.shop.bean.model.User;
import com.kimo.shop.bean.param.*;
import com.kimo.shop.bean.vo.UnDeliveryOrderExcelVO;
import com.kimo.shop.common.util.PageAdapter;
import com.kimo.shop.common.util.PageParam;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

public interface OrderMapper extends BaseMapper<Order> {

    Order getOrderByOrderNumber(@Param("orderNumber") String orderNumber);

    List<Order> listUnRefundOrderAndOrderItems(@Param("orderStatus") Integer orderStatus, @Param("lessThanUpdateTime") DateTime lessThanUpdateTime);

    void cancelOrders(@Param("orders") List<Order> orders);

    void receiptOrder(@Param("orders") List<Order> orders);

    void updateByToPaySuccess(@Param("orderNumbers") List<String> orderNumbers, @Param("payType") Integer payType);

    /**
     * 根据订单号获取订单及订单项
     *
     * @param orderNumber
     * @return
     */
    Order getOrderAndOrderItemByOrderNumber(@Param("orderNumber") String orderNumber);

    List<Order> listOrdersDetialByOrder(@Param("order") Order order, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    List<Order> listOrdersDetialByOrderParam(@Param("adapter") PageAdapter adapter, @Param("orderParam") OrderParam orderParam);

    Long countOrderDetial(@Param("orderParam") OrderParam orderParam);

    UserShoppingDataDto calculateUserInShopData(@Param("shopId") Long shopId, @Param("userId") String userId);

    List<MyOrderDto> listMyOrderByUserIdAndStatus(@Param("adapter") PageAdapter adapter, @Param("userId") String userId, @Param("status") Integer status,
                                                  @Param("dbLang") Integer dbLang);

    Long countMyOrderByUserIdAndStatus(@Param("userId") String userId, @Param("status") Integer status, @Param("isComm") Integer isComm, @Param("stationId") Long stationId);

    void deleteOrders(@Param("orders") List<Order> orders);


    OrderPayParam getOrderCountByShopId(@Param("shopId") Long shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    List<OrderPayParam> getActualTotalByHour(@Param("shopId") Long shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    List<OrderPayParam> getActualTotalByDay(@Param("shopId") Long shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    List<OrderPayParam> getPayOrderByTime(@Param("shopId") Long shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);


    OrderCountData getOrderCount(String userId);

    void cancelSeckillOrderByTime(@Param("now") Date now);

    /**
     * 更新订单为待成团
     */
    int updateToWaitGroup(@Param("orderNumber") String orderNumber);

    /**
     * 通过拼团团队id更新订单为已发货状态
     */
    int updateToWaitDelivery(@Param("groupTeamId") Long groupTeamId);

    /**
     * 通过拼团团队id获取所有关联订单
     * @param groupTeamId 拼团团队id
     * @return 关联订单
     */
    List<Order> selectOrderByGroupTeamId(@Param("groupTeamId") Long groupTeamId);

    List<MyOrderDto> orderListByStatusAndStationId(@Param("adapter") PageAdapter pageAdapter, @Param("status") Integer status, @Param("stationId") Long stationId);

    /**
     * 通过用户id和订单编号获取订单
     */
    Order getOrderByOrderNumberAndUserId(@Param("orderNumber") String orderNumber, @Param("userId") String userId);


    int hasBuySuccessProd(@Param("prodId") Long prodId, @Param("userId") String userId);

    List<Order> listOrdersDetialByOrderInfo(@Param("orderParam") OrderParam orderParam);

    Order getOrderDetailByOrderNumberAndShopId(@Param("orderNumber") String orderNumber, @Param("shopId") Long shopId, @Param("lang") Integer dbLang);

    List<Order> getOrderPayInfoByOrderNumber(@Param("orderNumberList") List<String> orderNumberList);

    /**
     * 模糊查询订单列表中的某一项订单
     */
    List<MyOrderDto> listMyOrderByParams(@Param("adapter") PageAdapter adapter, @Param("userId") String userId, @Param("status") Integer status,
                                         @Param("orderName") String orderName, @Param("orderTimeStatus") Integer orderTimeStatus,
                                         @Param("preTime") String preTime, @Param("orderType") Integer orderType, @Param("orderNumber") String orderNumber,
                                         @Param("lang") Integer dbLang);

    /**
     * 统计模糊查询订单列表中的某一项订单
     */
    Long countMyOrderByParams(@Param("userId") String userId, @Param("status") Integer status, @Param("orderName") String orderName,
                              @Param("orderTimeStatus") Integer orderTimeStatus, @Param("preTime") String preTime,
                              @Param("orderType") Integer orderType, @Param("orderNumber") String orderNumber);

    List<MyOrderDto> OrderCommentByUserIdAndStatus(@Param("adapter") PageAdapter pageAdapter, @Param("userId") String userId, @Param("isComm") Integer isComm);

    List<MyOrderItemDto> orderItemCommentByUserIdAndStatus(@Param("adapter") PageAdapter pageAdapter, @Param("userId") String userId,
                                                           @Param("isComm") Integer isComm);

    long countOrderItemComment(@Param("userId") String userId, @Param("isComm") Integer isComm);

    List<MyOrderDto> getOrderListByStationId(@Param("stationId") Long stationId, @Param("userId") String userId);

    List<Order> getStationOrderByOrderNumbers(@Param("orderNumberList") List<String> orderNumberList, @Param("userId") String userId, @Param("stationId") Long stationId);

    List<Order> getOrderAndOrderItemByOrderNumberList(@Param("orderNumberList") Set<String> orderNumberList);

    /** 统计时间内，成功下单的商品件数之和（不剔除退款订单） */
    Integer countOrderNum(@Param("param") ProdAnalysisSurveyParam param);
    /** 统计时间内， 成功付款订单的商品件数之和（不剔除退款订单） */
    Integer countPayNum(@Param("param") ProdAnalysisSurveyParam param);
    /** 统计时间内， 成功付款订单的人数和（不剔除退款订单，去重userId） */
    Integer countPayCustomerNum(@Param("param") CustomerReqParam param);

    Integer countMemberPayNum(@Param("param") CustomerReqParam param);
    /** 新成交客户数：过去n(beforeYear:过去n年的时间点)年没有购买，在筛选时间内首次在店铺付款的客户数量 */
    Integer countNewMemberPayNum(@Param("beforeYear") Date beforeYear, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
    /** 老成交客户数：过去n(beforeYear:过去n年的时间点)年没有购买，在筛选时间内在店铺再次付款的客户数量 */
    Integer countOldMemberPayNum(@Param("beforeYear") Date beforeYear, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    Double countMemberPayAmount(@Param("param") CustomerReqParam param);
    /** 新成交客户数：过去n(beforeYear:过去n年的时间点)年没有购买，在筛选时间内首次在店铺付款金额总和*/
    Double countNewMemberPayAmount(@Param("beforeYear") Date beforeYear, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
    /** 老成交客户数：过去n(beforeYear:过去n年的时间点)年没有购买，在筛选时间内在店铺再次付款金额总和 */
    Double countOldMemberPayAmount(@Param("beforeYear") Date beforeYear, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    Integer countPayNumRFM(@Param("type") Integer type, @Param("f") Integer f, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    Double sumPayAmountRFM(@Param("type") Integer type, @Param("f") Integer f, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
    /** type = 0 新客户 其他老客户 */
    Integer countNewOrOldPayProdNum(@Param("type") Integer type, @Param("beforeYear") Date beforeYear, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
    /** type = 0 新客户 其他老客户 */
    Integer countNewOrOldPayTimes(@Param("type") Integer type, @Param("beforeYear") Date beforeYear, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    List<CustomerOrderParam> getNewRepurchaseParam(@Param("shopId") Long shopId, @Param("type") Integer type, @Param("beforeYear") Date beforeYear, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    CustomerRepurchaseDetailParam getNewRepurchaseCount(@Param("shopId") Long shopId, @Param("type") Integer type, @Param("cycle") Integer cycle, @Param("userId") String userId, @Param("payTime") Date payTime, @Param("beforeYear") Date beforeYear, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
    /** 会员支付数 or 订单数*/
    Integer countPaidMemberByParam(@Param("param") MemberReqParam param, @Param("isOrderNum") Integer isOrderNum);

    Double countMemberPaidAmount(@Param("param") MemberReqParam param);

    Integer countMemberPayOrder(@Param("param") MemberReqParam param);

    Integer countPayMemberNum(@Param("param") MemberReqParam param);
    /** type = 0 新成交会员  */
    Integer countNewOrOldMemberPayOrder(@Param("type") Integer type, @Param("beforeYear") Date beforeYear, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
    /** type = 0 新成交会员  */
    Integer countNewMemberEveryDayPayNum(@Param("type") Integer type,@Param("beforeYear") Date beforeYear, @Param("startTime") Date startTime, @Param("dayStartTime") Date dayStartTime, @Param("dayEndTime") Date dayEndTime);

    Integer countNewOrOldMemberEveryDayPayPayOrder(@Param("type") Integer type, @Param("beforeYear") Date beforeYear, @Param("startTime") Date startTime, @Param("dayStartTime") Date dayStartTime, @Param("dayEndTime") Date dayEndTime);

    Double countNewMemberEveryDayPayAmount(@Param("type") Integer type, @Param("beforeYear") Date beforeYear, @Param("startTime") Date startTime, @Param("dayStartTime") Date dayStartTime, @Param("dayEndTime") Date dayEndTime);

    List<String> countPayUser(@Param("startTime") Date startTime, @Param("endTime") Date endTime);
    /** type = 0 排除已经支付过的用户  1 不排除*/
    List<String> countPayUserRetained(@Param("type") Integer type, @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("userIds") List<String> userIds);

    List<CustomerRFMRespTableParam> getUserOrderCrateTime(@Param("type") Integer type, @Param("mouth") Date mouth, @Param("threeMouth") Date threeMouth, @Param("halfYear") Date halfYear, @Param("year") Date year);
    List<String> listUserIdByPurchaseNum(@Param("isPayed") Integer isPayed,
                                         @Param("deleteStatus") Integer deleteStatus,
                                         @Param("startDate") Date startDate,
                                         @Param("endDate") Date endDate,
                                         @Param("minNum") Long minNum,
                                         @Param("maxNum") Long maxNum);

    List<String> listUserIdByAverageActualTotal(@Param("isPayed") Integer isPayed,
                                                @Param("deleteStatus") Integer deleteStatus,
                                                @Param("startDate") Date startDate,
                                                @Param("endDate") Date endDate,
                                                @Param("minAmount") BigDecimal minAmount,
                                                @Param("maxAmount") BigDecimal maxAmount);

    Date getLastConsumeDateByUserId(@Param("userId") String userId);
    /**  type = 0 不统计余额支付部分 type = 1 统计*/
    Double countConsumeAmount(@Param("userId") String userId, @Param("type") Integer type);

    Integer countPayNumByUserId(@Param("userId") String userId);

    Double countReduceAmountByUserId(String userId);

    IPage<Order> getPageByUserId(@Param("page") PageParam<Order> page, @Param("userId") String userId);

    List<ProdAnalysisDataParam> prodSurveyList(@Param("param") ProdAnalysisSurveyParam param);

    long countMyOrderByUserIdAndComm(@Param("userId") String userId, @Param("isComm") Integer isComm);

    /**
     * 获取订单的创建状态，给秒杀提供查询是否已经创建订单成功的功能
     *
     * @param orderNumber 订单id
     * @return
     */
    Integer countByOrderNumber(@Param("orderNumber") String orderNumber);

    /**
     * 获取用户的成交留存率, 统计用户第一次成交后，之后的1到6个月的留存用户数
     * @param param 条件查询参数
     * @return 成交留存信息列表
     */
    List<CustomerRetainRespParam> getTradeRetained(@Param("param") CustomerRetainedReqParam param);

    /**
     * 获取待发货订单列表
     *
     * @param orderParam 订单参数
     * @return 待发货订单列表
     */
    List<UnDeliveryOrderExcelVO> listUnDeliveryOrderExcel(@Param("orderParam") OrderParam orderParam);

    /**
     * 根据订单号和商家id获取信息
     * @param orderNumber 订单编号
     * @param shopId 店铺id
     * @return 信息
     */
    UnDeliveryOrderExcelVO getOrderAndOrderItemsByOrderNumberAndShopId(@Param("orderNumber") String orderNumber, @Param("shopId") Long shopId);

    /**
     *根据店铺id按订单状态分组获取订单数量
     * @param shopId
     * @return
     */
    OrderCountData getOrderCountOfStatusByShopId(@Param("shopId") Long shopId);

    /**
     * 获取在指定时间范围内消费了指定金额的用户
     * @param minAmount
     * @param maxAmount
     * @param startDate
     * @param endTime
     * @return
     */
    List<User> listUserByConsAmount(@Param("minAmount") BigDecimal minAmount, @Param("maxAmount") BigDecimal maxAmount, @Param("startDate") Date startDate, @Param("endTime") Date endTime);
}
